<template>
  <div class="todo-list">
    <h2>自律一下</h2>
    <div class="input-container">
      <input v-model="newTodoTitle" @keyup.enter="addTodo" placeholder="添加新的待办事项" />
      <button @click="addTodo">添加</button>
    </div>
    <TodoItem
      v-for="(todo, index) in todos"
      :key="index"
      :title="todo.title"
      :completed="todo.completed"
      @update:completed="updateTodoCompleted(index, $event)"
      @remove-todo="removeTodo(index)"
    />
  </div>
</template>

<script>
import TodoItem from './TodoItem.vue';

export default {
  name: 'TodoList',
  components: {
    TodoItem
  },
  data() {
    return {
      todos: [
        { title: '学习100分钟', completed: false },
        { title: '游泳60分钟', completed: false },
        { title: '跑步30分钟', completed: false }
      ],
      newTodoTitle: ''
    };
  },
  methods: {
    addTodo() {
      if (this.newTodoTitle.trim()) {
        this.todos.push({ title: this.newTodoTitle, completed: false });
        this.newTodoTitle = '';
      }
    },
    removeTodo(index) {
      this.todos.splice(index, 1);
    },
    updateTodoCompleted(index, completed) {
      this.todos[index].completed = completed;
    }
  }
};
</script>

<style scoped>
.todo-list {
  max-width: 400px;
  margin: 0 auto;
  padding: 20px;
  border: 1px solid #ccc;
  border-radius: 5px;
}

.input-container {
  display: flex;
  align-items: center;
}

input {
  width: calc(100% - 80px); 
  padding: 10px;
  margin-bottom: 10px;
}

button {
  width: 13%; 
  padding: 10px;
  margin-left: 5px; 
}
</style>